#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Reset Image Config                                                 #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 50
#
# MANUAL: This script will replace all 2dx_image_cfg files of the selected images with the 2dx_master.cfg file.
# MANUAL: Please note that all the parameters except imagename, nonmaskimagename, movie_stackname, imagenumber, imagename_original will be replaced.
#
# RESET: reset_sure1 = "no"
# RESET: reset_sure2 = "no"
# RESET: reset_warning = "Make no mistake here!"
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set imagename = ""
set reset_sure1 = ""
set reset_sure2 = ""
#
#$end_vars
#
set scriptname = 2dx_reset_image_cfg
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
echo "<<@progress: 1>>"
#
if ( ${reset_sure1} == "no" || ${reset_sure2} == "no" ) then
  ${proc_2dx}/protest "Not running (Are you sure is 'no')."
endif
#
set oldir = $PWD
#
set dirnum = `cat 2dx_merge_dirfile.dat | wc -l`
echo ": Will work on ${dirnum} image directories."
set increment = `echo ${dirnum} | awk '{ s = 70.0 / ( $1 + 1 ) } END { print s }'`
set curval = 20.0
set dircounter = ${dirnum}
while ( ${dircounter} > 0 )
    #
    ${bin_2dx}/2dx_getline.exe << eot > TMP.tmp
${oldir}/2dx_merge_dirfile.dat
${dircounter}
eot
    #
    set to_dir = `cat TMP.tmp`
    \rm TMP.tmp
    cd ${oldir}
    cd ..
    cd ${to_dir}
    #
    # Variables not to reset
    set imagename = `grep "set imagename =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set nonmaskimagename = `grep "set nonmaskimagename =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set movie_stackname = `grep "set movie_stackname =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set imagenumber = `grep "set imagenumber =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set imagename_original = `grep "set imagename_original =" 2dx_image.cfg | head -1 | cut -d'"' -f2`
    set movie_stackname_raw = `grep "set movie_stackname_raw = " 2dx_image.cfg | head -1 | cut -d'"' -f2`
    #
    # Backup the file
    \cp -f 2dx_image.cfg 2dx_image.cfg-backup4
    #
    # Copy the file
    set fullpath = `pwd`
    echo "::Copying ${oldir}/2dx_merge.cfg to ${fullpath}/2dx_image.cfg"
    \cp -f ${oldir}/2dx_merge.cfg ${fullpath}/2dx_image.cfg
    #
    # Following tells GUI that the parameters for image have been changed and the GUI should reload them.
    echo "<RESETDIR="\"${fullpath}\"">" >> ${oldir}/LOGS/${scriptname}.results
    #
    # Reset the variables
    #
    # This tells the GUI that the parameters are now to be added in the current image directory
    echo "<IMAGEDIR="\"${fullpath}\"">" >> ${oldir}/LOGS/${scriptname}.results
    #
    echo ": Updating - imagename to ${imagename}"
    echo 'set imagename = "'${imagename}'"' >> ${oldir}/LOGS/${scriptname}.results
    echo ": Updating - nonmaskimagename to ${nonmaskimagename}"
    echo 'set nonmaskimagename = "'${nonmaskimagename}'"' >> ${oldir}/LOGS/${scriptname}.results
    echo ": Updating - movie_stackname to ${movie_stackname}"
    echo 'set movie_stackname = "'${movie_stackname}'"' >> ${oldir}/LOGS/${scriptname}.results
    echo ": Updating - imagenumber to ${imagenumber}"
    echo 'set imagenumber = "'${imagenumber}'"' >> ${oldir}/LOGS/${scriptname}.results
    echo ": Updating - imagename_original to ${imagename_original}"
    echo 'set imagename_original = "'${imagename_original}'"' >> ${oldir}/LOGS/${scriptname}.results
    echo ": Updating - movie_stackname_raw to ${movie_stackname_raw}"
    echo 'set movie_stackname_raw = "'${movie_stackname_raw}'"' >> ${oldir}/LOGS/${scriptname}.results
    #
    echo "set initialization_reset = y" >> ${oldir}/LOGS/${scriptname}.results
    echo "set initialization_executable = y" >> ${oldir}/LOGS/${scriptname}.results
    #
    cd ${oldir}
    #
    # This tells the GUI to get back to the original dir for setting parameters
    echo "<IMAGEDIR>" >> ${oldir}/LOGS/${scriptname}.results
    #
    set curval = `echo ${curval} ${increment} | awk '{ s = $1 + $2 } END { print s }'`
    set curprogress = `echo ${curval} | awk '{ s = int( $1 ) } END { print s }'`
    echo "<<@progress:${curprogress}>>"
    #
    @ dircounter -= 1
    cd ${oldir}
end
#
echo ":: "
echo ":: "
echo ":: Done."
echo ":: "
echo ":: All image parameter databases in the files 2dx_image.cfg were reset to the default parameters for this project."
echo ":: If this was a mistake, you can still use the Backup or Restore Databases script to recover the last versions."
echo ":: "

echo "<<@progress: 100>>"
#
exit
#
